﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Scoot.UI.Conversation
{
    /// <summary>
    /// Interaction logic for Dialog.xaml
    /// </summary>
    /// 
    
    public partial class DialogTab : TabItem
    {
        public delegate void CloseTabEventHandler(DialogTab tab);
        public event CloseTabEventHandler Close_Tab;
        private FlowDocument Document=new FlowDocument();

        public delegate void SendMessageHandler(int uin, string message);
        public event SendMessageHandler Send_Message;
        public int uin;
        public DialogTab(int uin)
        {
            InitializeComponent();
            this.UIN.Text = uin.ToString();
            this.uin = uin;
            Conversation.Document = Document;
        }
        public void addMessage(string message, bool external=true)
        {
            if (external)
            {
                Paragraph _content = new Paragraph();
                _content.Inlines.Add(new Bold(new Run(uin.ToString() + ": ")));
                _content.Inlines.Add(new Run(message));
                _content.Margin = new Thickness(0);
                Document.Blocks.Add(_content);
                Conversation.ScrollToEnd();
            }
            else
            {
                Paragraph _content = new Paragraph();
                _content.Inlines.Add(new Bold(new Run("Me: ")));
                _content.Inlines.Add(new Run(message));
                _content.Margin = new Thickness(0);
                Document.Blocks.Add(_content);
                Conversation.ScrollToEnd();
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            Close_Tab(this);
        }

        private void SendMessage_Click(object sender, RoutedEventArgs e)
        {
            Send_Message(uin, InputBox.Text);
            addMessage(InputBox.Text, false);
            InputBox.Text = "";
            
        }

        private void InputBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Send_Message(uin, InputBox.Text);
                addMessage(InputBox.Text, false);
                InputBox.Text = "";
            }
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Middle)
                Close_Tab(this);
        }

    }
}
